package com.nowar.common.result.response;


import com.nowar.common.constant.CommonResultCode;
import com.nowar.common.util.SnowFlakeUtil;
import lombok.Getter;
import lombok.Setter;

/**
 * 公共响应
 *
 * @Author: 谢无殇。
 * @Date : 2023-01-15 21:21
 */

@Getter
@Setter
public class ResponseCommonBase<T> {

    /**
     * 响应码
     */
    private String resultCode;
    /**
     * 响应信息
     */
    private String resultMessage;
    /**
     * 流水号
     */
    private String transactionId;
    /**
     * 响应体
     */
    private T data;

    public static ResponseCommonBase getResponseBase() {
        ResponseCommonBase responseBase = new ResponseCommonBase();
        responseBase.setResultCode(CommonResultCode.OK.getCode());
        responseBase.setResultMessage(CommonResultCode.OK.getMessage());
        return responseBase;
    }

    public static ResponseCommonBase ok() {
        ResponseCommonBase responseBase = getResponseBase();
        responseBase.setTransactionId(String.valueOf(SnowFlakeUtil.nextId()));
        return responseBase;
    }


    public static ResponseCommonBase ok(String transactionId) {
        ResponseCommonBase responseBase = getResponseBase();
        responseBase.setTransactionId(transactionId);
        return responseBase;
    }

    public static ResponseCommonBase ok(Object data) {
        ResponseCommonBase responseBase = getResponseBase();
        responseBase.setTransactionId(String.valueOf(SnowFlakeUtil.nextId()));
        responseBase.setData(data);
        return responseBase;
    }

    public static ResponseCommonBase ok(String transactionId, Object data) {
        ResponseCommonBase responseBase = getResponseBase();
        responseBase.setTransactionId(transactionId);
        responseBase.setData(data);
        return responseBase;
    }


    public static ResponseCommonBase error() {
        return error(String.valueOf(SnowFlakeUtil.nextId()));
    }

    public static ResponseCommonBase error(String transactionId) {
        ResponseCommonBase responseBase = new ResponseCommonBase();
        responseBase.setResultCode(CommonResultCode.ERROR.getCode());
        responseBase.setResultMessage(CommonResultCode.ERROR.getMessage());
        responseBase.setTransactionId(transactionId);
        return responseBase;
    }

    public static ResponseCommonBase error(String resultCode, String resultMessage) {
        ResponseCommonBase responseBase = new ResponseCommonBase();
        responseBase.setResultCode(resultCode);
        responseBase.setResultMessage(resultMessage);
        responseBase.setTransactionId(String.valueOf(SnowFlakeUtil.nextId()));
        return responseBase;
    }

    public static ResponseCommonBase error(String resultCode, String resultMessage, String transactionId) {
        ResponseCommonBase responseBase = new ResponseCommonBase();
        responseBase.setResultCode(resultCode);
        responseBase.setResultMessage(resultMessage);
        responseBase.setTransactionId(transactionId);
        return responseBase;
    }
}
